import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    goodList: [
      {
        name: "诸葛亮",
        price: 1000,
        num: 1,
        checked: false,
      },
      {
        name: "蔡文姬",
        price: 1500,
        num: 1,
        checked: false,
      },
      {
        name: "妲己",
        price: 2000,
        num: 1,
        checked: false,
      },
      {
        name: "鲁班",
        price: 2200,
        num: 1,
        checked: false,
      },
    ],
  },
  getters: {
    // 小选状态计算价格总和
    sumPrice(state){
      return  state.goodList.reduce((sum,item) =>{
        if(item.checked === true){
          sum += item.price * item.num
        }
        return sum 
      },0)
    }
    
  },
  mutations: {
    // 删除事件
    remove(state, index) {
      state.goodList.splice(index, 1);
    },
    // 输如框改变事件
    changeInput(state, index) {
      state.goodList[index].checked = !state.goodList[index].checked;
    },
    // 全选选小选
    IsAllChange(state, val) {
      state.goodList.forEach((item) => (item.checked = val));
    },
    //数量减少
    reduceNum(state,index){
      state.goodList[index].num -= 1
    },
    addNum(state,index){
      state.goodList[index].num += 1
    }
  },
  actions: {},
  modules: {},
});
